package kr.saeildamil.androidstudy_second33;

import java.io.IOException;

import kr.saeildamil.androidstudy_second.R;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

public class PlayVideo extends Activity implements SurfaceHolder.Callback{
	private SurfaceView mPreview;
	private SurfaceHolder mHolder;
	MediaPlayer mPlayer;
	Button mPlayBtn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch33_playvideo);
		
		mPreview=(SurfaceView) findViewById(R.id.surface);
		mHolder=mPreview.getHolder();
		mHolder.addCallback(this);
		
		mPlayBtn=(Button) findViewById(R.id.play);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		if(mPlayer!=null){
			mPlayer.release();
			mPlayer=null;
		}
	}
	
	public void mOnClick(View v){
		switch (v.getId()) {
		case R.id.play:
			if(mPlayer.isPlaying()==false){
				mPlayer.start();
				mPlayBtn.setText("Pause");
			}else{
				mPlayer.pause();
				mPlayBtn.setText("Play");
			}
			break;

		case R.id.stop:
			mPlayer.stop();
			try {
				mPlayer.prepare();
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		}
	}
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		
	}
	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		if(mPlayer==null){
			mPlayer=new MediaPlayer();
		}else{
			mPlayer.reset();
		}
		String sd=Environment.getExternalStorageDirectory().getAbsolutePath();
		try {
			mPlayer.setDataSource(sd+"/Movies/스트라이크 백 시즌3/Strike.Back.S03E01E02.avi");
			mPlayer.setDisplay(holder);
			mPlayer.prepare();
			mPlayer.setOnCompletionListener(mComplete);
			mPlayer.setOnVideoSizeChangedListener(mSizeChange);
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		
	}
	
	MediaPlayer.OnCompletionListener mComplete=new MediaPlayer.OnCompletionListener() {
		
		@Override
		public void onCompletion(MediaPlayer mp) {
			mPlayBtn.setText("Play");
		}
	}; 
	MediaPlayer.OnVideoSizeChangedListener mSizeChange=new MediaPlayer.OnVideoSizeChangedListener() {
		
		@Override
		public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
			
		}
	};
	
}
